package com.cloudwise.controller;



import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.cloudwise.entity.ReturnBean;
import com.cloudwise.entity.Speciality;
import com.cloudwise.service.SpecialityService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * (Speciality)表控制层
 *
 * @author makejava
 * @since 2022-01-21 17:12:13
 */
@RestController
@RequestMapping("speciality")
public class SpecialityController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private SpecialityService specialityService;

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param speciality 查询实体
     * @return 所有数据
     */
    @GetMapping("selectAll")
    public ReturnBean selectAll(Integer page, Integer limit, Speciality speciality) {
        Page<Speciality> specialityPage = new Page<>(page,limit);
        QueryWrapper<Speciality> queryWrapper = new QueryWrapper<>();
        if(ObjectUtil.isNotEmpty(speciality.getSpecialityName())){
            queryWrapper.like("speciality_name",speciality.getSpecialityName());
        }

        Page<Speciality> page1 = this.specialityService.page(specialityPage,queryWrapper);

        return super.success(page1.getRecords(),page1.getTotal());
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ReturnBean selectOne(@PathVariable Serializable id) {
        return success(this.specialityService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param speciality 实体对象
     * @return 新增结果
     */
    @PostMapping("insert")
    public ReturnBean insert(@RequestBody Speciality speciality) {
        boolean save = specialityService.save(speciality);
        if(save){
            return success(speciality,1L);
        }else {
            return fail(speciality,1L);
        }
    }

    /**
     * 修改数据
     *
     * @param speciality 实体对象
     * @return 修改结果
     */
    @PutMapping("update")
    public ReturnBean update(@RequestBody Speciality speciality) {
        boolean updateById = specialityService.updateById(speciality);
        if(updateById){
            return super.success(speciality,1L);
        }else {
            return super.fail(speciality,1L);
        }
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping("delete")
    public ReturnBean delete(@RequestParam(value = "idList[]",required = false) List<Long> idList) {
        boolean delete = this.specialityService.removeByIds(idList);
        if (delete) {
            return super.success(null,"操作成功");
        } else {
            return super.fail(null,"操作失败");
        }
    }
}

